import org.junit.Test;

import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 * @author Yin_JC
 * @PackageName:PACKAGE_NAME
 * @Class:NIOtest
 * @Description:
 * @date 2020/3/6 8:34
 */

public class NIOTest {
    @Test
    public void test1(){
        String a = "殷嘉铖";
        ByteBuffer buf = ByteBuffer.allocate(1024);
        showInfo(buf);

        buf.put(a.getBytes());
        showInfo(buf);

        buf.flip();
        showInfo(buf);

        byte[] bb = new byte[buf.limit()];
        buf.get(bb);
        System.out.println(Arrays.toString(bb));

        showInfo(buf);

        buf.clear();
        showInfo(buf);

        buf.put(a.getBytes());
        buf.flip();
        byte[] b3 = new byte[buf.limit()];
        buf.get(b3,0,2);
        System.out.println(Arrays.toString(b3));
        showInfo(buf);
        buf.get(b3,2,4);
        System.out.println(Arrays.toString(b3));
        showInfo(buf);
    }

    public void showInfo(ByteBuffer buf){
        System.out.println("------------------------------");
        System.out.println(buf.position());
        System.out.println(buf.limit());
        System.out.println(buf.capacity());
    }
}
